<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Site extends Common
{
    // 网站设置
    public function index() {
        $site = db('site')->select();
        $data = [];
        foreach ($site as $v) {
            $data[$v['name']] = $v['value'];
        }
        $this->assign('site', $data);

        return view();
    }

    // 基本信息编辑
    public function save_info() {
//        Db::listen(function($sql, $time, $explain){
//            // 记录SQL
//            echo $sql. ' ['.$time.'s]';
//            // 查看性能分析结果
//            dump($explain);
//        });
        // 先删除原来的数据
        db('site')->where('`name` = "title" OR `name` = "keywords" OR `name` = "description" OR `name` = "footer" OR `name` = "kefu"')->delete();

        // 添加新数据
        $data = [
            ['name' => 'title', 'value' => input('param.title')],
            ['name' => 'keywords', 'value' => input('param.keywords')],
            ['name' => 'description', 'value' => input('param.description')],
            ['name' => 'footer', 'value' => input('param.footer')],
            ['name' => 'kefu', 'value' => input('param.kefu', 0)]
        ];
        $rs = db('site')->insertAll($data);

        if ($rs > 0)
            return json(['code' => 200, 'msg' => '网站基本信息编辑成功']);
        else
            return json(['code' => 0, 'msg' => '网站基本信息编辑失败']);
    }

}
